package deep1jvmdvm;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * *******************************************************
 * Author: chinadragon
 * Time: 2022/5/11 上午8:05
 * Name: VIP3, jvm01  运行时数据区
 * Overview:
 * Usage:
 *
 * HSDB的使用
 * https://blog.csdn.net/qq_33983753/article/details/121357509
 *
 * 44 MacOs启动HSDB碰到的几个问题
 * https://www.csdn.net/tags/OtDaUgzsMTA5MTYtYmxvZwO0O0OO0O0O.html
 *
 *
 * HSDB 在mac下的启动和使用
 * https://www.jianshu.com/p/9a26e85a2482
 *
 *
 * 找不到或无法加载主类 sun.jvm.hotspot.HSDB
 * https://blog.csdn.net/hjing123/article/details/109743335
 *
 * jclasslib 插件安装及使用
 * https://blog.csdn.net/m0_47988201/article/details/120220435
 *
 * 使用Jclasslib分析字节码中的方法内部结构
 * https://www.cnblogs.com/yuexiaoyun/articles/13998443.html
 *
 * *******************************************************
 */
public class VIP3Jvm01 {
    public static void main(String[] args) {
        /*
            一个方法对应一个栈栈
            JVM，一个栈开辟空间大小是1M

            JVM是个规范，Dalivk、ART虚拟机算是定制，可以更改栈开辟空间大小


            栈溢出 和 内存溢出 是两码事

            StackOverflowErro 栈溢出 栈内部空间不足

            OOM 堆区  内存空间不足，不能申请内存了


            HSDB工具是 JDK里的 lib 目录 sa-jdi.jar自带的工具

            jsp命令 专门查看 java 进程，类似Linux ps -ef | grep java
         */
        work1();
        work2();
    }

    /*
        使用 jclasslib 插件 分析局部变量
     */

    private static void work1() {
        int a = 10;
        int b = 20;
    }

    private static void work2() {
        int c = 30;
        int d = 40;
    }

    private void method1() {
        int i = 50;
        int j = 60;
        int k = i + j;
        double numb = 66.35;
    }

    private int method2() {
        int i = 50;
        int j = 60;
        int k = i + j;
        double numb = 66.35;

        return k;
    }

    private void readFile() {
        try {
            FileReader fileReader = new FileReader(new File("test.txt"));
            char[] b = new char[4096];
            int length = -1;
            while ((length = fileReader.read(b)) > 0){
                String s = new String(b);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        /*
            异常表：
            跳转PC 51 ，查看LineNumberTable表 51 对应的 行号：74

         */
    }

}

/*
 :test chinadragon$ cd ../
:src chinadragon$ cd deep1jvmdvm
:deep1jvmdvm chinadragon$ javac VIP3Jvm01.java
:deep1jvmdvm chinadragon$ javap -v VIP3Jvm01.class
Classfile /Users/chinadragon/Desktop/java_project/src/deep1jvmdvm/VIP3Jvm01.class
  Last modified 2022年5月11日; size 431 bytes
  SHA-256 checksum 080d6965bd4ec0029d34a8a706daaa075e723b7787b9e21cdbabe4b98b4ff470
  Compiled from "VIP3Jvm01.java"
public class deep1jvmdvm.VIP3Jvm01
  minor version: 0
  major version: 60
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #8                          // deep1jvmdvm/VIP3Jvm01
  super_class: #2                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 4, attributes: 1
Constant pool:
   #1 = Methodref          #2.#3          // java/lang/Object."<init>":()V
   #2 = Class              #4             // java/lang/Object
   #3 = NameAndType        #5:#6          // "<init>":()V
   #4 = Utf8               java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Methodref          #8.#9          // deep1jvmdvm/VIP3Jvm01.work1:()V
   #8 = Class              #10            // deep1jvmdvm/VIP3Jvm01
   #9 = NameAndType        #11:#6         // work1:()V
  #10 = Utf8               deep1jvmdvm/VIP3Jvm01
  #11 = Utf8               work1
  #12 = Methodref          #8.#13         // deep1jvmdvm/VIP3Jvm01.work2:()V
  #13 = NameAndType        #14:#6         // work2:()V
  #14 = Utf8               work2
  #15 = Utf8               Code
  #16 = Utf8               LineNumberTable
  #17 = Utf8               main
  #18 = Utf8               ([Ljava/lang/String;)V
  #19 = Utf8               SourceFile
  #20 = Utf8               VIP3Jvm01.java
{
  public deep1jvmdvm.VIP3Jvm01();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 12: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=1, args_size=1
         0: invokestatic  #7                  // Method work1:()V
         3: invokestatic  #12                 // Method work2:()V
         6: return
      LineNumberTable:                        // 局部变量表
        line 17: 0
        line 18: 3
        line 19: 6
}
SourceFile: "VIP3Jvm01.java"
:deep1jvmdvm chinadragon$



        分析：
             LineNumberTable:                        // 局部变量表

             LocalVariableTable：
             是数组，里面的序号会跳过，因为是32位，包括引用类型的也是32位，如果是64位的占用两个字节，所以从 5 跳到7

             名字列表里有的有this 因为 该方法不是静态方法，静态方法里就没有this


             private void method1(){
                int i = 50;
                int j = 60;
                int k = i + j;
                double numb = 66.35;
            }

            metthod1方法  字节码表解说：

             bipush表示将数据放入操作数栈 - 入栈

             istore_0表示将数据从操作数栈移出放入局部变量表第 0 位置
             istore_1表示将数据从操作数栈移出放入局部变量表第 1 位置

             iload_1 表示加载 i
             iload_2 表示加载 j

             iadd 执行 i + j

             istore_3 ------



             就是汇编操作指令

 */


/*
    javap -v -p  VIP3Jvm01.class

    java jdk1.8之后支持动态链接


   :deep1jvmdvm chinadragon$ javap -v -p  VIP3Jvm01.class
Classfile /Users/chinadragon/Desktop/java_project/src/deep1jvmdvm/VIP3Jvm01.class
  Last modified 2022年5月11日; size 431 bytes
  SHA-256 checksum 080d6965bd4ec0029d34a8a706daaa075e723b7787b9e21cdbabe4b98b4ff470
  Compiled from "VIP3Jvm01.java"
public class deep1jvmdvm.VIP3Jvm01
  minor version: 0
  major version: 60
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #8                          // deep1jvmdvm/VIP3Jvm01
  super_class: #2                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 4, attributes: 1
Constant pool:                                                                              --------注意 常量池
   #1 = Methodref          #2.#3          // java/lang/Object."<init>":()V
   #2 = Class              #4             // java/lang/Object
   #3 = NameAndType        #5:#6          // "<init>":()V
   #4 = Utf8               java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Methodref          #8.#9          // deep1jvmdvm/VIP3Jvm01.work1:()V
   #8 = Class              #10            // deep1jvmdvm/VIP3Jvm01
   #9 = NameAndType        #11:#6         // work1:()V
  #10 = Utf8               deep1jvmdvm/VIP3Jvm01
  #11 = Utf8               work1
  #12 = Methodref          #8.#13         // deep1jvmdvm/VIP3Jvm01.work2:()V
  #13 = NameAndType        #14:#6         // work2:()V
  #14 = Utf8               work2
  #15 = Utf8               Code
  #16 = Utf8               LineNumberTable
  #17 = Utf8               main
  #18 = Utf8               ([Ljava/lang/String;)V
  #19 = Utf8               SourceFile
  #20 = Utf8               VIP3Jvm01.java
{
  public deep1jvmdvm.VIP3Jvm01();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 12: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=1, args_size=1
         0: invokestatic  #7                  // Method work1:()V
         3: invokestatic  #12                 // Method work2:()V
         6: return
      LineNumberTable:
        line 17: 0
        line 18: 3
        line 19: 6

  private static void work1();
    descriptor: ()V
    flags: (0x000a) ACC_PRIVATE, ACC_STATIC
    Code:
      stack=1, locals=2, args_size=0
         0: bipush        10
         2: istore_0
         3: bipush        20
         5: istore_1
         6: return
      LineNumberTable:
        line 22: 0
        line 23: 3
        line 24: 6

  private static void work2();
    descriptor: ()V
    flags: (0x000a) ACC_PRIVATE, ACC_STATIC
    Code:
      stack=1, locals=2, args_size=0
         0: bipush        30
         2: istore_0
         3: bipush        40
         5: istore_1
         6: return
      LineNumberTable:
        line 27: 0
        line 28: 3
        line 29: 6
}
SourceFile: "VIP3Jvm01.java"
:deep1jvmdvm chinadragon$


 */